﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_Inventory_TransactionRegister_PurchaseReturn : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    string finyear = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindFormat();
            BindGrid(txtFromDate.Text, txtToDate.Text);
        }
    }

    protected void BindFormat()
    {
        string strsql = "select TFOID,FORMATNAME from IVR_TRANFORMAT where trantype=6";
        DataTable dtformat = DU.getdata(strsql);
        ddlFormatName.DataSource = dtformat;
        ddlFormatName.DataTextField = "FORMATNAME";
        ddlFormatName.DataValueField = "TFOID";
        ddlFormatName.DataBind();
    }

    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                      new SqlParameter("@TRANTYPE","6"),
                                  };

            dtMain = DU.getdatausingprocedure("USP_IVR_GetTransactionRegisterMain", sqlparam);
        }
        DataView dvDtl = dtMain.DefaultView;
        string strtfoid = "TFOID =" + ddlFormatName.SelectedValue.ToString();
        dvDtl.RowFilter = strtfoid;
        DataTable dtMainFinal = dvDtl.ToTable();
        dtlstPurchaseReturn.DataSource = dtMainFinal;
        dtlstPurchaseReturn.DataBind();
        //dtlstPurchaseReturn1.DataSource = dtMainFinal;
        //dtlstPurchaseReturn1.DataBind();
        if (dtMainFinal.Rows.Count > 0)
        {
            dtlstPurchaseReturn.Visible = true;
            pnlmsg.Visible = false;
        }
        else
        {
            dtlstPurchaseReturn.Visible = false;
            pnlmsg.Visible = true;
        }
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void dtlstPurchaseReturn_ItemBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbl = (Label)e.Item.FindControl("lblDate");
            GridView grd = (GridView)e.Item.FindControl("grdPurchaseReturnDtl");
            ImageButton imgShow = (ImageButton)e.Item.FindControl("imgShow");

            string trnDate = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(lbl.Text));

            SqlParameter[] sqlparam = {
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtFromDate.Text))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtToDate.Text))),
                                      new SqlParameter("@TRANTYPE","6"),
                                      };

            DataTable dtDetails = DU.getdatausingprocedure("USP_IVR_GetTransactionRegisterDetails", sqlparam);
            DataView dvDetails = dtDetails.DefaultView;
            string strFiltrExpr = "TFOID =" + ddlFormatName.SelectedValue.ToString();
            dvDetails.RowFilter = strFiltrExpr;
            DataTable dtDtlScnd = dvDetails.ToTable();
            DataView dvDtlScnd = dtDtlScnd.DefaultView;
            string strFilterExpr = "TRNDATE =#" + trnDate + "#";
            // string strFilterExpr = "TFOID =" + ddlFormatName.SelectedValue.ToString();
            dvDtlScnd.RowFilter = strFilterExpr;
            DataTable dtDtlFinal = dvDtlScnd.Table;
            grd.DataSource = dtDtlFinal;
            grd.DataBind();

            grd.RowDataBound += new GridViewRowEventHandler(grd_RowDataBound);
            imgShow.Attributes.Add("onclick", "return expandDetailsInGrid('" + imgShow.ClientID + "','" + grd.ClientID + "');");
        }
    }

    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double qty = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMQTY"));
            Double rate = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "RATE"));
            Double value = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMVALUE"));
            Double discount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "DISCAMT"));
            Double tax = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TAXAMT"));
            Double total = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMAMT"));
            if (qty > 0)
            {
                e.Row.Cells[2].Text = acntstng.GetCurrency(qty);
            }
            else
            {
                e.Row.Cells[2].Text = string.Empty;
            }
            if (rate > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(rate);
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }
            if (value > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(value);
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (discount > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(discount);
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (tax > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(tax);
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
            if (total > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(total);
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }

            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
        }
    }

    protected void ddlFormatName_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    #region /*Commented datalist for print*/
    //protected void dtlstPurchaseReturn1_ItemBound(object sender, DataListItemEventArgs e)
    //{
    //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    //    {
    //        Label lbl1 = (Label)e.Item.FindControl("lblDate1");
    //        GridView grd1 = (GridView)e.Item.FindControl("grdPurchaseReturnDtl1");

    //        string trnDate1 = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(lbl1.Text));

    //        SqlParameter[] sqlparam = {
    //                                  new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtFromDate.Text))),
    //                                  new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtToDate.Text))),
    //                                  new SqlParameter("@TRANTYPE","6"),
    //                                  };

    //        DataTable dtDetails1 = DU.getdatausingprocedure("USP_IVR_GetTransactionRegisterDetails", sqlparam);
    //        DataView dvDetails1 = dtDetails1.DefaultView;
    //        string strFiltrExpr1 = "TFOID =" + ddlFormatName.SelectedValue.ToString();
    //        dvDetails1.RowFilter = strFiltrExpr1;
    //        DataTable dtDtlScnd1 = dvDetails1.ToTable();
    //        DataView dvDtlScnd1 = dtDtlScnd1.DefaultView;
    //        string strFilterExpr1 = "TRNDATE =#" + trnDate1 + "#";
    //        // string strFilterExpr = "TFOID =" + ddlFormatName.SelectedValue.ToString();
    //        dvDtlScnd1.RowFilter = strFilterExpr1;
    //        DataTable dtDtlFinal1 = dvDtlScnd1.Table;
    //        grd1.DataSource = dtDtlFinal1;
    //        grd1.DataBind();
    //        grd1.RowDataBound += new GridViewRowEventHandler(grd1_RowDataBound);
    //    }
    //}
    //protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        Double qty1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMQTY"));
    //        Double rate1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "RATE"));
    //        Double value1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMVALUE"));
    //        Double discount1 =Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "DISCAMT"));
    //        Double tax1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TAXAMT"));
    //        Double total1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ITMAMT"));
    //        if (qty1 > 0)
    //        {
    //            e.Row.Cells[2].Text = acntstng.GetCurrency(qty1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[2].Text = string.Empty;
    //        }
    //        if (rate1 > 0)
    //        {
    //            e.Row.Cells[3].Text = acntstng.GetCurrency(rate1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[3].Text = string.Empty;
    //        }
    //        if (value1 > 0)
    //        {
    //            e.Row.Cells[4].Text = acntstng.GetCurrency(value1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[4].Text = string.Empty;
    //        }
    //        if (discount1 > 0)
    //        {
    //            e.Row.Cells[5].Text = acntstng.GetCurrency(discount1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[5].Text = string.Empty;
    //        }
    //        if (tax1 > 0)
    //        {
    //            e.Row.Cells[6].Text = acntstng.GetCurrency(tax1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[6].Text = string.Empty;
    //        }
    //        if (total1 > 0)
    //        {
    //            e.Row.Cells[7].Text = acntstng.GetCurrency(total1);
    //        }
    //        else
    //        {
    //            e.Row.Cells[7].Text = string.Empty;
    //        }

    //        e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
    //        e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
    //        e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
    //        e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
    //        e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
    //        e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
    //    }
    //}
    #endregion
}
